package com.bsb.biz.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.bsb.base.model.BsbTaxrate;
import com.bsb.base.model.BsbTaxrateExample;
import com.bsb.biz.service.BsbTaxrateService;
import com.bsb.biz.shiro.UserUtils;

@Controller
@RequestMapping("/bsbTaxrate")
public class BsbTaxrateController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/bsbTaxrate/list.do";
	
	@Resource
	private BsbTaxrateService bsbTaxrateService;
	
	@RequiresPermissions("bsbTaxrate:view")
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public String list(
			Model model,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		BsbTaxrateExample bsbTaxrateExample = new BsbTaxrateExample();
		int totalCount = bsbTaxrateService.countBsbTaxrate(bsbTaxrateExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		bsbTaxrateExample.setPageIndex((pageNum - 1) * pageSize);
		bsbTaxrateExample.setPageSize(pageSize);
		bsbTaxrateExample.setOrderByClause("insertTime DESC");
		List<BsbTaxrate> bsbTaxrates = bsbTaxrateService.getPageBsbTaxrate(bsbTaxrateExample);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("bsbTaxrates", bsbTaxrates);
		return "/BsbTaxrate/list";
	}
	
	@RequiresPermissions("bsbTaxrate:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String toAdd(Model model) {
		return "/BsbTaxrate/add";
	}

	@RequiresPermissions("bsbTaxrate:create")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String add(Model model, @ModelAttribute("bsbTaxrate") BsbTaxrate bsbTaxrate) {
		bsbTaxrate.setInsertId(UserUtils.currentUser().getId().toString());
		bsbTaxrateService.addBsbTaxrate(bsbTaxrate);
		return redirect;
	}
	
	@RequiresPermissions("bsbTaxrate:delete")
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	public String del(Model model,RedirectAttributes redirectAttributes,
			HttpServletRequest request,
			@PathVariable("id") Integer id) {
		String queryString = request.getQueryString();
		bsbTaxrateService.delBsbTaxrateById(id);
		return redirect+ "?" +queryString;
	}
	
	@RequiresPermissions("bsbTaxrate:update")
	@RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
	public String toEdit(Model model,RedirectAttributes redirectAttributes,
			HttpServletRequest request,
			@PathVariable("id") Integer id) {
		BsbTaxrate bsbTaxrate = bsbTaxrateService.getBsbTaxrateById(id);
		String queryString = request.getQueryString();
		model.addAttribute("queryString", queryString);
		model.addAttribute("bsbTaxrate", bsbTaxrate);
		return "/BsbTaxrate/edit";
	}

	@RequiresPermissions("bsbTaxrate:update")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String edit(Model model,@RequestParam String queryString,
			@ModelAttribute("bsbTaxrate") BsbTaxrate bsbTaxrate) {
		bsbTaxrate.setUpdateId(UserUtils.currentUser().getId().toString());
		bsbTaxrateService.updateBsbTaxrate(bsbTaxrate);
		return redirect+ "?" +queryString;
	}
	
	@RequiresPermissions("bsbTaxrate:view")
	@RequestMapping(value = "/{id}/view", method = RequestMethod.GET)
	public String view(Model model,
			@PathVariable("id") Integer id) {
		BsbTaxrate bsbTaxrate = bsbTaxrateService.getBsbTaxrateById(id);
		model.addAttribute("bsbTaxrate", bsbTaxrate);
		return "/BsbTaxrate/view";
	}
}
